﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Windows.Forms;

namespace CIS470MetricsTracking
{
	public class ProjectController
	{
		private Dictionary<string, Project> projects;

		public ProjectController()
		{
			projects = new Dictionary<string, Project>();
			string query = "select projectName, projectStart, projectEndProjected, projectDesc from dboProject";
			DataSet ds = new DataSet();
			DataTable dt = new DataTable();

			ds = ControllerManager.data_controller.executeSelectQuery(query);

			if (ds != null) {
				if (ds.Tables.Count != 0) {
					dt = ds.Tables[0];
				}
				if (dt.Rows.Count != 0) {
					foreach (DataRow dr in dt.Rows) {
						Project proj = new Project(dr.ItemArray[0].ToString(), dr.ItemArray[1].ToString(),
							dr.ItemArray[2].ToString(), dr.ItemArray[3].ToString());
						projects.Add(proj.getProjectName(), proj);
					}
				}
			} else {
				MessageBox.Show("Critical Data Access Failure!", "Critical Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				Application.Exit();
			}
		}

		// Returns true if project is successfully created false if it is not.
		public bool create_project(string project_name, string project_start, string project_end, string project_desc)
		{
			string query = "";
			DataSet ds = new DataSet();
			DataTable dt = new DataTable();

			query = "select userId from dboUser where email='" +
				ControllerManager.user_controller.getUserEmail() + "'";

			ds = ControllerManager.data_controller.executeSelectQuery(query);
			dt = ds.Tables[0];

			Project proj = new Project(project_name, project_start, project_end, project_desc);
			projects.Add(project_name, proj);

			int uid = (int)dt.Rows[0].ItemArray[0];

			query = "insert into dboProject (projectName, projectStart, projectEndProjected, projectDesc, userId) VALUES ('" +
				project_name + "', '" + proj.getProjectStart() + "', '" +
				proj.getProjectEnd() + "', '" + proj.getProjectDesc() + "', " + uid + ")";

			if (!ControllerManager.data_controller.executeQuery(query)) {
				return false;
			}

			return true;
		}

		// Returns a project from the Dictionary selected by the project name
		// Returns null if the project can not be found
		public Project getProject(string project_name)
		{
			if (projects.ContainsKey(project_name)) {
				return projects[project_name];
			} else {
				return null;
			}
		}

		public Dictionary<string, Project> load_projects()
		{
			string query = "select projectName, projectStart, projectEndProjected, projectDesc from dboProject";
			DataSet ds = new DataSet();
			DataTable dt = new DataTable();

			ControllerManager.data_controller.executeSelectQuery(query);

			if (ds != null) {
				if (ds.Tables.Count != 0) {
					dt = ds.Tables[0];
				}
				if (dt.Rows.Count != 0) {
					foreach (DataRow dr in dt.Rows) {
						Project proj = new Project(dr.ItemArray[0].ToString(), dr.ItemArray[1].ToString(),
							dr.ItemArray[2].ToString(), dr.ItemArray[3].ToString());
						projects.Add(proj.getProjectName(), proj);
					}
				}
			} else {
				MessageBox.Show("Critical Data Access Failure!", "Critical Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				Application.Exit();
			}

			return projects;
		}

		public Dictionary<string, Project> getProjects()
		{
			return projects;
		}

		// Return true if the project does not exist in the database already.
		public bool validate_project(string project_name)
		{
			string query = "";
			DataSet ds = new DataSet();
			DataTable dt = new DataTable();

			query = "select projectName from dboProject where projectName='" + project_name + "'";
			ds = ControllerManager.data_controller.executeSelectQuery(query);

			if (ds != null) {
				if (ds.Tables.Count != 0) {
					dt = ds.Tables[0];
				}
				if (dt.Rows.Count == 0) {
					return true;
				} else {
					return false;
				}
			} else {
				return false;
			}
		}
		
		// Returns a id of 0 when there is a issue as no projectId can be 0
		public int getProjectId(string project_name)
		{
			string query = "select projectId from dboProject where projectName='" + project_name + "'";
			DataSet ds = new DataSet();
			DataTable dt = new DataTable();

			ds = ControllerManager.data_controller.executeSelectQuery(query);
			if (ds != null) {
				if (ds.Tables.Count != 0) {
					dt = ds.Tables[0];
					return (int)dt.Rows[0].ItemArray[0];
				} else {
					return 0;
				}
			} else {
				return 0;
			}
		}

		public int calculate_report_period(string start_date)
		{
			string[] temp = start_date.Split('/');
			int start_month = Int32.Parse(temp[0]);
			DateTime date = DateTime.Now;
			int current_month = date.Month;

			if (start_month == current_month) {
				return 1;
			} else {
				return (current_month - start_month) + 1;
			}
		}
	}
}
